!> \brief Contains subroutines to extract from a file some data in the form
!! "desired_data matching_string"

module data_collector

   implicit none

contains

   !===============================================================================

   !> \brief Scans a file named filename looking for a string str.
   !! If a line with str was found, then it reads the first number
   !! of that line and store the result in n. The subroutine
   !! returns res = 1 for success and res = 0 for failure.
   subroutine get_an_integer_from_a_file(filename,str,n,res)
      implicit none
      character (len=*), intent(in) :: filename !< Input file name
      character (len=*), intent(in) :: str      !< String to find
      integer, intent(out) :: n   !< Read number
      integer, intent(out) :: res !< Result status

      ! Auxiliary variables

      integer :: IO, idx

      character (len=500) :: line

      res = 0

      n = 0

      open(10,file=filename)

      do

         read(10,"(A)",iostat=IO) line

         if ( IO /= 0 ) exit

         ! Cheking for str in line

         idx = index(line,str)

         if ( idx /= 0 ) then

            backspace(10)

            read(10,*,iostat=IO) n

            if ( IO /= 0 ) then

               res = 0

            else

               res = 1

            end if

            exit

         end if

      end do

      close(10)

   end subroutine

   !===============================================================================

   !> \brief Scans a file named filename looking for a string str.
   !! If a line with str was found, then it reads the first number
   !! of that line and store the result in x. The subroutine
   !! returns res = 1 for success and res = 0 for failure.
   subroutine get_a_double_from_a_file(filename,str,x,res)
      implicit none
      character (len=*), intent(in) :: filename !< Input file name
      character (len=*), intent(in) :: str      !< String to find
      real(8), intent(out) :: x !< Read number
      integer, intent(out) :: res !< Result status

      ! Auxiliary variables

      integer :: IO, idx

      character (len=500) :: line

      res = 0

      x = 0.d0

      open(10,file=filename)

      do

         read(10,"(A)",iostat=IO) line

         if ( IO /= 0 ) exit

         ! Cheking for str in line

         idx = index(line,str)

         if ( idx /= 0 ) then

            backspace(10)

            read(10,*,iostat=IO) x

            if ( IO /= 0 ) then

               res = 0

            else

               res = 1

            end if

            exit

         end if

      end do

      close(10)

   end subroutine

   !===============================================================================

   !> \brief Scans a file named filename looking for a string str.
   !! If a line with str was found, then it reads the first string
   !! of that line and store the result in string. The subroutine
   !! returns res = 1 for success and res = 0 for failure.
   subroutine get_a_string_from_a_file(filename,str,string,res)
      implicit none
      character (len=*), intent(in)  :: filename !< Input file name
      character (len=*), intent(in)  :: str      !< String to find
      character (len=*), intent(out) :: string   !< String found
      integer, intent(out) :: res !< Result status

      ! Auxiliary variables

      integer :: IO, idx

      character (len=500) :: line

      res = 0

      string = ""

      open(10,file=filename)

      do

         read(10,"(A)",iostat=IO) line

         if ( IO /= 0 ) exit

         ! Cheking for str in line

         idx = index(line,str)

         if ( idx /= 0 ) then

            backspace(10)

            read(10,*,iostat=IO) string

            if ( IO /= 0 ) then

               res = 0

            else

               res = 1

            end if

            exit

         end if

      end do

      close(10)

   end subroutine

   !===============================================================================

end module
